﻿//-----------------------------------------------------------------------
// <copyright file="GenerateFilterCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.PrivateMessages
{
    using System.Collections.Generic;

    using ChatBox.UI.Commands;
    using ChatBox.UI.MessageFilters;

    /// <summary>
    /// command to generate a filter from the data on the filter view model
    /// </summary>
    public class GenerateFilterCommand : GenericCommandBase<FilterViewModel>
    {
        /// <summary>
        /// Generate the filter
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(FilterViewModel value)
        {
            List<IMessageFilter> filters = new List<IMessageFilter>();

            if (value.SelectedGroupIndex > 0)
            {
                filters.Add(new GroupMessageFilter(value.SelectedGroup));
            }

            if (value.SelectedSenderIndex > 0)
            {
                filters.Add(new FromMessageFilter(value.SelectedSender));
            }

            if (!string.IsNullOrEmpty(value.Text))
            {
                filters.Add(new TextMessageFilter(value.Text));
            }

            if (value.MaxDate.HasValue)
            {
                filters.Add(new DateFilter(value.MaxDate.Value));
            }

            if (filters.Count == 0)
            {
                value.CurrentFilter = null;
            }
            else if (filters.Count == 1)
            {
                value.CurrentFilter = filters[0];
            }
            else
            {
                value.CurrentFilter = new CollectionMessageFilter(filters);
            }
        }
    }
}
